//
//  KVViewController.swift
//  test-ios
//
//  Created by wyz5204 on 2023/2/28.
//

import UIKit
import MMKV
/**
 这里主要对ios本身的Key-Value的库进行学习和使用 同时添加MVVM库的开发
 */
class KVViewController: UIViewController {
    
    let mmkv = MMKV.default()
    
    lazy var writeButton = {
        return UIButton().then { view in
            view.setTitleAndColor("存数据")
        }
    }()
    
    lazy var readButton = {
        return UIButton().then { view in
            view.setTitleAndColor("读数据")
        }
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        writeButton.addTo(self.view) { [weak self] view in
            view.snp.makeConstraints { make in
                make.top.equalToSuperview().offset(100)
            }
        }
        
        readButton.addTo(self.view) { [weak self] view in
            view.snp.makeConstraints { make in
                make.top.equalToSuperview().offset(200)
            }
        }
        
        writeButton.setTapListener { [weak self]button in
            print("开始 写入 ")
            
            self?.mmkv?.set(true, forKey: "bool")
            self?.mmkv?.set("hello, mmkv", forKey: "string")
            self?.mmkv?.set(Int32.init(-100), forKey: "int")
            
        }.disposed(by: rx.disposeBag)
        
        readButton.setTapListener { [weak self]button in
            print("开始 读取 " ,self?.mmkv?.bool(forKey: "bool") ,
                  self?.mmkv?.int32(forKey: "int") ,
                  self?.mmkv?.string(forKey: "string")
            )
        }.disposed(by: rx.disposeBag)
        
    }
    
}
